package com.relatev.minecraft.TRAttribute;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

public class TAttribute implements Listener, Runnable {

    public List<String> effectiveTexts;
    public final ConcurrentHashMap<LivingEntity, ConcurrentHashMap<String, List<AttributeValue>>> valueMap = new ConcurrentHashMap();
    public final ConcurrentHashMap<LivingEntity, ResultValue> cacheValueMap = new ConcurrentHashMap();

    public TAttribute(List<String> texts) {
        effectiveTexts = texts;
    }

    public void onEnable() {

    }

    public void onDisable() {

    }

    private ResultValue resultCache(LivingEntity lent) {
        ResultValue rv;
        if (valueMap.containsKey(lent) == false) {
            rv = new ResultValue(this);
        } else {
            double plusr = 0;
            double rater = 0;
            double multiplyr = 1;
            for (List<AttributeValue> vlist : valueMap.get(lent).values()) {
                for (AttributeValue attrv : vlist) {
                    switch (attrv.Type) {
                        case PLUS:
                            plusr = plusr + attrv.Value;
                            break;
                        case MULTIPLY:
                            multiplyr = multiplyr * attrv.Value;
                            break;
                        case RATE:
                            rater = rater + attrv.Value;
                            break;
                    }
                }
            }
            final double fplus = plusr;
            final double frate = rater;
            final double fmultiply = multiplyr;
            rv = new ResultValue(this) {
                {
                    plus = fplus;
                    rate = frate;
                    multiply = fmultiply;
                }
            };
        }
        cacheValueMap.put(lent, rv);
        return rv;
    }

    public ResultValue result(LivingEntity lent) {
        ResultValue rv = cacheValueMap.get(lent);
        if (rv == null) {
            rv = resultCache(lent);
            return rv;
        } else {
            return rv;
        }
    }

    public double result(double plus, double rate, double multiply) {
        return plus * (1 + rate) * multiply;
    }

    public void putValues(LivingEntity lent, List<AttributeValue> values, String tag) {
        ConcurrentHashMap<String, List<AttributeValue>> map;
        if (valueMap.containsKey(lent)) {
            map = valueMap.get(lent);
        } else {
            map = new ConcurrentHashMap();
            valueMap.put(lent, map);
        }
        map.put(tag, values);
        cacheValueMap.remove(lent);
    }

    public void removeValues(LivingEntity lent, String tag) {
        if (valueMap.containsKey(lent)) {
            valueMap.get(lent).remove(tag);
        }
        cacheValueMap.remove(lent);
    }

    @Override
    public void run() {
        Iterator<LivingEntity> it = valueMap.keySet().iterator();
        while (it.hasNext()) {
            LivingEntity lent = it.next();
            if (lent instanceof Player) {
                if (((Player) lent).isOnline() == false) {
                    it.remove();
                }
            } else if (lent.isValid() == false) {
                it.remove();
            }
        }
    }

    public class ResultValue {

        public ResultValue(TAttribute attr) {
            attribute = attr;
        }

        public final TAttribute attribute;
        public double plus = 0;
        public double rate = 0;
        public double multiply = 1;

        public double result() {
            return attribute.result(plus, rate, multiply);
        }
    }
}
